package itsmap.systematic.exam;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ItsmapSystematicProjecttActivity extends Activity {
    
	private final int mlogin = 1;
	private Restapi restapi;
	private final String loginDone = "LoginPreference";
	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
		  @Override
		  public void onReceive(Context context, Intent intent) {
		    // Get extra data included in the Intent
		    			  
			String message = intent.getStringExtra("message");
			boolean loginTrueFalse = Boolean.parseBoolean(message);
		    Log.d("receiver", "Got message: " + message);
		    
		    if(loginTrueFalse)
		    	startActivity(new Intent().setClass(ItsmapSystematicProjecttActivity.this, PatientListActivity.class));
		    else
		    {
		    	Toast.makeText(ItsmapSystematicProjecttActivity.this, "login Error Try agian", Toast.LENGTH_SHORT).show();
		    	startActivity(new Intent().setClass(ItsmapSystematicProjecttActivity.this, ItsmapSystematicProjecttActivity.class));
		    }
		    
		  }
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startscreen);
        restapi = new Restapi();
    }
    
    public void welcomeBtn(View view){
    	
    	SharedPreferences setting = getSharedPreferences(loginDone, Activity.MODE_PRIVATE);
    	SharedPreferences.Editor settingsEditor = setting.edit();
    	
    	settingsEditor.putBoolean(loginDone,true);
    	settingsEditor.commit();
    	
    	LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
    		      new IntentFilter("login"));
    	Intent i=new Intent(this, Restapi.class);
        
    	EditText userName = (EditText)findViewById(R.id.loginTextInput);
    	EditText passWord = (EditText)findViewById(R.id.loginPasswordInput);
    	    	
        i.putExtra("action", mlogin);
        i.putExtra("username", userName.getText().toString());
        i.putExtra("password", passWord.getText().toString());
                
        startService(i);    	
    }
    
    //protected void onActivityResult(int requestCode, int resultCode, Intent data){
    	//if(resultCode == RESULT_OK && requestCode == mlogin){
    		//data.setClass(this,PatientListActivity.class);
    		//startActivity(data);
    	//}
    	
    //}
    
    @Override
    protected void onDestroy() {
      // Unregister since the activity is about to be closed.
      LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
      super.onDestroy();
    }
}